package com.lfy.modules.monitor;

import com.lfy.modules.common.config.sys.DictionariesKey;
import com.lfy.modules.common.config.sys.SysDictionaries;
import com.lfy.modules.common.custom.ApplicationStart;
import com.lfy.modules.common.config.thread.MyLinkedBlockingQueue;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Optional;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池配置初始化
 * @author lvfeiyang
 */
@Component
public class ThreadPoolRefresh implements ApplicationStart {

    @Resource(name = "mainThreadPool")
    private ThreadPoolExecutor mainThreadPool;
    @Override
    public void init() {
        Optional.ofNullable(SysDictionaries.getValue(DictionariesKey.SYS_THREAD_CORE)).map(Integer::parseInt).ifPresent(mainThreadPool::setCorePoolSize);
        Optional.ofNullable(SysDictionaries.getValue(DictionariesKey.SYS_THREAD_MAX)).map(Integer::parseInt).ifPresent(mainThreadPool::setMaximumPoolSize);
        Optional.ofNullable(SysDictionaries.getValue(DictionariesKey.SYS_THREAD_ALARM)).map(Integer::parseInt).ifPresent(ThreadPoolMontitor::setOpenAlarm);
        Optional.ofNullable(SysDictionaries.getValue(DictionariesKey.SYS_THREAD_QUEUE)).map(Integer::parseInt).ifPresent(queueSize -> {
            MyLinkedBlockingQueue<?> myLinkedBlockingQueue = (MyLinkedBlockingQueue<?>) mainThreadPool.getQueue();
            myLinkedBlockingQueue.setCapacity(queueSize);
        });
    }

    public static void main(String[] args) {
        ArrayList<Object> arrayList = new ArrayList<>();
        Object o = arrayList.get(0);
        System.out.println(o);
    }
}
